package com.shire.shire_interface.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class AppRandomUtils {
    public static final String BaseChar16 = "0123456789ABCDEF";
    public static final String BaseCharAll = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final String BaseNumber = "0123456789";

    public static final String IPHONE_SERIAL_CHAR = "123456789CDFGHJKLMNPQRSTVWXYZ";

    private static final List<KeyValue<Integer, Integer>> CARRIER_LIST = new ArrayList<KeyValue<Integer,Integer>>(){{
        add(new KeyValue<Integer, Integer>(134,1));
        add(new KeyValue<Integer, Integer>(135,1));
        add(new KeyValue<Integer, Integer>(136,1));
        add(new KeyValue<Integer, Integer>(137,1));
        add(new KeyValue<Integer, Integer>(138,1));
        add(new KeyValue<Integer, Integer>(139,1));
        add(new KeyValue<Integer, Integer>(147,1));
        add(new KeyValue<Integer, Integer>(150,1));
        add(new KeyValue<Integer, Integer>(151,1));
        add(new KeyValue<Integer, Integer>(152,1));
        add(new KeyValue<Integer, Integer>(157,1));
        add(new KeyValue<Integer, Integer>(158,1));
        add(new KeyValue<Integer, Integer>(159,1));
        add(new KeyValue<Integer, Integer>(178,1));
        add(new KeyValue<Integer, Integer>(182,1));
        add(new KeyValue<Integer, Integer>(183,1));
        add(new KeyValue<Integer, Integer>(184,1));
        add(new KeyValue<Integer, Integer>(187,1));
        add(new KeyValue<Integer, Integer>(188,1));

        add(new KeyValue<Integer, Integer>(130,2));
        add(new KeyValue<Integer, Integer>(131,2));
        add(new KeyValue<Integer, Integer>(132,2));
        add(new KeyValue<Integer, Integer>(155,2));
        add(new KeyValue<Integer, Integer>(156,2));
        add(new KeyValue<Integer, Integer>(185,2));
        add(new KeyValue<Integer, Integer>(186,2));
        add(new KeyValue<Integer, Integer>(145,2));
        add(new KeyValue<Integer, Integer>(176,2));

        add(new KeyValue<Integer, Integer>(133,3));
        add(new KeyValue<Integer, Integer>(153,3));
        add(new KeyValue<Integer, Integer>(177,3));
        add(new KeyValue<Integer, Integer>(180,3));
        add(new KeyValue<Integer, Integer>(181,3));
        add(new KeyValue<Integer, Integer>(189,3));

    }};

    private static String firstName="赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯咎管卢莫经房裘缪干解应宗宣丁贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄魏加封芮羿储靳汲邴糜松井段富巫乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘姜詹束龙叶幸司韶郜黎蓟薄印宿白怀蒲台从鄂索咸籍赖卓蔺屠蒙池乔阴郁胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍却璩桑桂濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庚终暨居衡步都耿满弘匡国文寇广禄阙东殴殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相查后江红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳淳于仲孙太叔申屠公孙乐正轩辕令狐钟离闾丘长孙慕容鲜于宇文司徒司空亓官司寇仉督子车颛孙端木巫马公西漆雕乐正壤驷公良拓拔夹谷宰父谷粱晋楚阎法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生岳帅缑亢况后有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福百家姓续";
    private static String girl="秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
    private static String boy="伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";

    public static String getRandomNum(int length){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(BaseNumber.length());
            if(i==0&&number==0){
                number = 3;
            }
            sb.append(BaseNumber.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomHex(int length){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(BaseChar16.length());
            if(i==0&&number==0){
                number = 3;
            }
            sb.append(BaseChar16.charAt(number));
        }
        return sb.toString();
    }

    // D323BEAB-8B86-4D4F-B27F-58CDCBDEE88A
    public static String genIdfa(){

        UUID uuid=UUID.randomUUID();
        return uuid.toString().toUpperCase();
//		Random random = new Random();
//	    StringBuffer sb = new StringBuffer();
//	    for (int i = 0; i < 36; i++) {
//	    	if(i==8 || i==13 || i==18 || i==23){
//	    		sb.append("-");
//	    		continue;
//	    	}
//	        int number = random.nextInt(BaseChar16.length());
//	        if(i==0 && number==0){
//	        	number = 1;
//	        }
//	        sb.append(BaseChar16.charAt(number));
//	    }
//	    return sb.toString();
    }

    public static String genUuid(){
        return genIdfa();
    }

    // imei 355245508691414
    public static String genImei(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer("3569");
        for (int i = 0; i < 11; i++) {
            int number = random.nextInt(BaseNumber.length());
//	        if(i==0 && number==0){
//	        	number = 3;
//	        }
            sb.append(BaseNumber.charAt(number));
        }
        return sb.toString();
    }

    // C36RF9ECFRC6
    // F17NTE3ZG5MT F17NTD3JG5MT F17NTEYWG5MT
    public static String genSerialNum(){
        Random random = new Random();
        int rand = random.nextInt(100);
        String prefix = "";
        if(rand<40){
            prefix = "C39Q";
        }
        else{
            prefix = "F17N";
        }
        StringBuffer sb = new StringBuffer(prefix);
        for (int i = 0; i < 8; i++) {
            int number = random.nextInt(BaseCharAll.length());
            sb.append(BaseCharAll.charAt(number));
        }
        return sb.toString();
    }

    public static String genSerialNum(String deviceType){
        if(deviceType==null) return genSerialNum();
        deviceType = deviceType.toLowerCase();
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        String prefix = "";
        switch (deviceType) {
            case "iphone5s":
                prefix = "C7JL";
                break;
            case "iphone6":
                prefix = "F17N";
                break;
            case "iphone6plus":
                prefix = "F17N";
                break;
            case "iphone6s":
                prefix = "C39Q";
                break;
            case "iphone6splus":
                prefix = "C39Q";
                break;
            case "iphone7":
                prefix = "DN2S";
                break;
            case "iphone7plus":
                prefix = "DN2S";
                break;
            case "iphone8":
                prefix = "C23V";
                break;
            case "iphone8plus":
                prefix = "C23V";
                break;
            case "iphonex":
                prefix = "G0NV";
                break;
            default:
                return genSerialNum();
        }

        sb.append(prefix);
        sb.append(IPHONE_SERIAL_CHAR.charAt(random.nextInt(22)+3));

        for (int i = 0; i < 7; i++) {
            int number = random.nextInt(BaseCharAll.length());
            sb.append(BaseCharAll.charAt(number));
        }
        return sb.toString();
    }

    public static Long genUniqueChipIdLong(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 16; i++) {
            int number = random.nextInt(BaseNumber.length());
            sb.append(BaseNumber.charAt(number));
        }
        Long v = StrUtils.getLongValue(sb.toString(), 0);
        return v;
    }

    // 1600114016492 3802364281876 13位 907810061305894
    public static String genUniqueChipId(){
//		Random random = new Random();
//	    StringBuffer sb = new StringBuffer();
//	    for (int i = 0; i < 16; i++) {
//	        int number = random.nextInt(BaseNumber.length());
////	        if(i==0 && number==0) number = 9;
//	        sb.append(BaseNumber.charAt(number));
//	    }
//
//	    return sb.toString();
//		max = 8841241789798074
//		min = 63536055853094
        Long v = null;
        for(int i=0; i<10; i++){
            v = genUniqueChipIdLong();
            if(v>=63536055853094L && v<=8841241789798074L){
                return ""+v;
            }
        }
        return ""+v;
    }

    public static String genUniqueChipId_wiki(){
        Random random = new Random();
        StringBuilder sb = new StringBuilder("44CC");
        for(int i=0; i<7; i++){
            sb.append(BaseChar16.charAt(random.nextInt(BaseChar16.length())));
        }
        return "" + Long.parseLong(sb.toString(), 16);
    }

    // F3Y41920JQ0FN64A
    public static String genMlb(){
        Random random = new Random();
        int rand = random.nextInt(100);
        String prefix = "";
        if(rand<30){
            prefix = "F3X";
        }
        else if(rand<60){
            prefix = "F3Y";
        }
        else{
            prefix = "DLX";
        }
        StringBuffer sb = new StringBuffer(prefix);
        for (int i = 0; i < 13; i++) {
            int number = random.nextInt(BaseNumber.length());
            sb.append(BaseNumber.charAt(number));
        }
        sb.append("F");
        return sb.toString();
    }

    // 4c:7c:5f:19:5b:dd
    public static String genMac(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 12; i++) {
            if(i>0 && i%2==0){
                sb.append(":");
            }
            int number = random.nextInt(BaseChar16.length());
            if(i==10 && number==15){
                number = 14;
            }
            sb.append(BaseChar16.charAt(number));
        }
        return sb.toString().toLowerCase();
    }

    public static String genNextMac(String mac){
        if(mac.length()!=17) return null;
        String macHex = mac.replace(":", "");
        Long nextLong = Long.parseLong(macHex, 16) + 1;
        String nextHex = Long.toHexString(nextLong);
        if(nextHex.length()<12){
            for(int i=0; i<12-nextHex.length(); i++){
                nextHex = "0" + nextHex;
            }
        }
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<nextHex.length(); i++){
            if(i>0 && i%2==0) sb.append(":");
            sb.append(nextHex.charAt(i));
        }
        return sb.toString().toLowerCase();
    }

    public static String genBatteryId(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer("LN");
        for (int i = 0; i < 16; i++) {
            if(i==9){
                sb.append("D");
                continue;
            }
            int number = random.nextInt(BaseNumber.length());
            sb.append(BaseNumber.charAt(number));
        }
        return sb.toString();
    }

    public static String genInverseDeviceid(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 40; i++) {
            int number = random.nextInt(BaseChar16.length());
            if(i==0){
                number = 6;
            }
            sb.append(BaseChar16.charAt(number));
        }
        return sb.toString().toLowerCase();
    }

    public static String genUdid(String serialId, String uniqueChipId, String wifiMac, String bluetoothMac){
//		char* data = "F17N82VTFR99" + "53933974996" + "24:e3:14:44:4d:77" + "24:e3:14:44:4d:78"
//		char[20] digest = [];
//		CC_SHA1(data.bytes, data.length, digest);
//		CFData data = CFDataCreate(nil, digest, 20);
        String data = serialId+uniqueChipId+wifiMac+bluetoothMac;
        data = SHA1.sha1(data);
//		System.out.println("data="+data);
        return data;
    }

    public static String genWifiSSID(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 10; i++) {
            if(i==3){
                sb.append("-");
                continue;
            }
            int number = random.nextInt(BaseCharAll.length());
            if(i==0){
                number = 4;
            }
            sb.append(BaseCharAll.charAt(number));
        }
        return sb.toString().toLowerCase();
    }

    public static String genWifiIp(){
        Random random = new Random();
        StringBuffer sb = new StringBuffer("192.168.");
        sb.append(random.nextInt(100));
        sb.append(".");
        sb.append(random.nextInt(220));
        return sb.toString();
    }

    public static KeyValue<String, Integer> genMobileNo(){
        Random random = new Random();
        int index = random.nextInt(CARRIER_LIST.size());
        KeyValue<Integer, Integer> prefix = CARRIER_LIST.get(index);
        StringBuilder sb = new StringBuilder(""+prefix.getKey());
        sb.append(getRandomNum(8));
        return new KeyValue<String, Integer>(sb.toString(), prefix.getValue());
    }


    public static String getChineseName() {
        Random random = new Random();
        int num = random.nextInt(2) + 1;
        char fn = firstName.charAt(random.nextInt(firstName.length()));
        StringBuilder sb = new StringBuilder();
        sb.append(fn);
        String nameStr = null;
        if(random.nextInt(100)<35){ // 女
            nameStr = girl;
        }
        else{
            nameStr = boy;
        }
        for(int i=0; i<num; i++){
            sb.append(nameStr.charAt(random.nextInt(nameStr.length())));
        }
        return sb.toString();
    }

    public static void testMain(String[] args){
        try {
            String s = "000E1CD9240A2026";
            Long ecid = 0x000E1CD9240A2026L;
            System.out.println("s="+Long.parseLong(s, 16));
            System.out.println("e="+ecid.longValue());

            String serialNumber = "F17NQE80G5MT";
            String uniqueChipId = ecid.longValue()+"";
            String wifiMac = "4c:7c:5f:19:5b:dc";
            String bluetoothMac = "4c:7c:5f:19:5b:dd";
            String data = genUdid(serialNumber, uniqueChipId, wifiMac, bluetoothMac);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        // ed8befa633a5b2f3907e703d7195002ff00d543c
        String serialId = "G0NVXS0SJCLF";
        String uniqueChipId = 0xE090E3804402EL + "";
        String wifiMac = "b8:c1:11:ab:88:f8";
        String bluetoothMac = "b8:c1:11:ab:88:f9";

        //54508ed6381496a763eb8000b599aab77c4d2e62
        serialId = "F17NF2604EJD";
        uniqueChipId = "4727854110989";
        wifiMac = "62:4e:22:73:e0:16";
        bluetoothMac = "62:4e:22:73:e0:17";

        System.out.println("ecid["+uniqueChipId.length()+"] = " + uniqueChipId);
        String udid = genUdid(serialId, uniqueChipId, wifiMac, bluetoothMac);
        System.out.println(udid);
        System.out.println();

    }
}
